//
//  ASBasicInfoVC.m
//  AttendenceSys
//
//  Created by 窦芳俊 on 16/1/24.
//  Copyright © 2016年 窦芳俊. All rights reserved.
//

#import "ASBasicInfoVC.h"
#import "ASInfoCell.h"
#import "ASNetworkEngine.h"
#import "SVProgressHUD.h"
#import "ASMyPortraitVC.h"
#import "ASMyHonorVC.h"
#import "ASPoliticalInfo.h"
#import "ASPoliticalInfoVC.h"

@interface ASBasicInfoVC () <UITableViewDataSource, UITableViewDelegate, ASInfoCellDelegate, ASPoliticalInfoVCDeleagete>

@property (nonatomic, strong) UITableView *infoTableView;
@property (nonatomic, strong) NSArray     *infoArray;
@property (nonatomic, assign) NSInteger    politicalNumber;

@end

@implementation ASBasicInfoVC

- (instancetype)initWithPolArray:(NSMutableArray *)array
{
    self = [super init];
    if (self)
    {
        if (_politicalArray == nil)
        {
            _politicalArray = [[NSMutableArray alloc] init];
        }
        _politicalArray = array;
        [ASAppDelegate sharedAppDelegate].politicalInfoArr = _politicalArray;
    }
    
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _politicalNumber = 4;
    
    [self setLeftButtonWithImageName:@"ic_back" bgImageName:nil];
    self.strNavTitle = @"基本信息";
    [self setRightText:@"保存"];
    
    //_infoArray = @[@"我的头像", @"名称", @"电话", @"邮箱", @"性别", @"出生年月", @"获奖成果"];
    _infoArray = @[@"姓名",@"班级",@"宿舍",@"政治面貌",@"联系方式"];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //[self fetchUserPolitical];
    
    [self initView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)initView
{
    UIImageView* bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.image = [UIImage imageNamed:@"bg_personal"];
    bgView.userInteractionEnabled = YES;
    [self.view addSubview:bgView];
    
    // infoTableView
    _infoTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _infoTableView.frame = CGRectMake(0, 0, self.view.frame.size.width, 250);
    _infoTableView.delegate = self;
    _infoTableView.dataSource = self;
    _infoTableView.backgroundColor = [UIColor whiteColor];
    _infoTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_infoTableView registerClass:[ASInfoCell class] forCellReuseIdentifier:NSStringFromClass([ASInfoCell class])];
    [bgView addSubview:_infoTableView];
}

//- (void)fetchUserPolitical
//{
//    // http://211.65.216.54/getPoliticalType.action
//    NSString *refer = @"getPoliticalType.action";
//    NSString *prefix = kASHttpUrl(refer);
//    
//    typeof(self) __weak weakSelf = self;
//    [SVProgressHUD showWithStatus:@"获取中..." maskType:SVProgressHUDMaskTypeClear];
//    ASNetworkEngine *networkingEngine = [ASNetworkEngine sharedInstance];
//    [networkingEngine GET:prefix parameters:nil success:^(id responseObject) {
//        NSDictionary* jsonRsp = (NSDictionary *)responseObject;
//        if (isSuccessRsp(jsonRsp))
//        {
//            NSArray* data = [jsonRsp objectForKey:kASJsonData];
//            for (NSInteger i = 0; i < [data count]; i++)
//            {
//                NSDictionary* dic = [data objectAtIndex:i];
//                ASPoliticalInfo *info  = [[ASPoliticalInfo alloc] init];
//                info.political = [[dic objectForKey:@"political"] integerValue];
//                info. politicalName = [dic objectForKey:@"politicalName"];
//                [weakSelf.politicalArray addObject:info];
//            }
//        }
//        [SVProgressHUD dismiss];
//    } failure:^(NSError *error) {
//        //[SVProgressHUD showErrorWithStatus:error.description];
//        [SVProgressHUD showErrorWithStatus:@"获取政治面貌失败"];
//    }];
//}

- (void)rightButtonPressed:(id)action
{
    [self modifyBasicInfo];
}

- (void)modifyBasicInfo
{
    //修改用户信息接口 http://211.65.216.54/modifyUserByCode.action?userCode=xiaohu&userName=111&userMobile=15295749809&userEmail=601640129@qq.com&sex=M&birthday=1989-12-03&portraitNum=1&dormitory=1&political=1 sex: M表示男 F表示女 更新什么就传什么 dormitory宿舍 political政治面貌 （getPoliticalType.action这个接口中获取）
    
    // 班级
    ASInfoCell *classCell = (ASInfoCell *)[_infoTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    NSString *userClass = classCell.rightText.text;
    
    // 宿舍
    ASInfoCell *dormitoryCell = (ASInfoCell *)[_infoTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
    NSString *userDormitory = dormitoryCell.dormitoryA.text;
    NSString *userDormitoryB = dormitoryCell.dormitoryB.text;
    userDormitory = [NSString stringWithFormat:@"%@#%@", userDormitory, userDormitoryB];
    
    // 政治面貌
    //ASInfoCell *politicalCell = (ASInfoCell *)[_infoTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];;
    //NSString *userPolitical = politicalCell.rightText.text;
    NSString *userPoliticalNumber = [NSString stringWithFormat:@"%ld", (long)_politicalNumber];
    // 联系方式
    ASInfoCell *teleCell = (ASInfoCell *)[_infoTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:4 inSection:0]];;
    NSString *userTele = teleCell.rightText.text;
    
    NSString *refer = @"modifyUserByCode.action?";
    NSString *prefix = kASHttpUrl(refer);
    NSString *userCode = [ASAppDelegate sharedAppDelegate].userInfo.userCode;
    NSString *totalUrl = [NSString stringWithFormat:@"%@userCode=%@&className=%@&dormitory=%@&political=%@&userMobile=%@",
                          prefix, userCode, userClass, userDormitory, userPoliticalNumber, userTele];
    
    [SVProgressHUD showWithStatus:@"处理中..." maskType:SVProgressHUDMaskTypeClear];
    ASNetworkEngine *networkingEngine = [ASNetworkEngine sharedInstance];
    [networkingEngine GET:totalUrl parameters:nil success:^(id responseObject) {
        NSDictionary* jsonRsp = (NSDictionary *)responseObject;
        if (isSuccessRsp(jsonRsp))
        {
            [SVProgressHUD showSuccessWithStatus:@"修改成功"];
            
            // 更新内存中的值
            [ASAppDelegate sharedAppDelegate].userInfo.className = userClass;
            [ASAppDelegate sharedAppDelegate].userInfo.dormitory = userDormitory;
            [ASAppDelegate sharedAppDelegate].userInfo.political = userPoliticalNumber;
            [ASAppDelegate sharedAppDelegate].userInfo.userMobile = userTele;
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:errorMsgRsp(jsonRsp)];
        }
    } failure:^(NSError *error) {
        //[SVProgressHUD showErrorWithStatus:error.description];
        [SVProgressHUD showErrorWithStatus:@"网络异常，请稍后再试"];
    }];
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
//    if (0 == [indexPath section])
//    {
//        if (0 == [indexPath row])
//        {
//            // 我的头像
//            //ASMyPortraitVC* myPortraitVC = [[ASMyPortraitVC alloc] init];
//            //myPortraitVC.hidesBottomBarWhenPushed = YES;
//            //[self.navigationController pushViewController:myPortraitVC animated:YES];
//        }
//    }
//    else if (1 == [indexPath section])
//    {
//        if (0 == [indexPath row])
//        {
//            // 获奖成果
//            ASMyHonorVC* myHonorVC = [[ASMyHonorVC alloc] init];
//            myHonorVC.hidesBottomBarWhenPushed = YES;
//            [self.navigationController pushViewController:myHonorVC animated:YES];
//        }
//    }
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //if (0 == section)
    {
        return 5;
    }
    
    //return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ASInfoCell class])];
    
    ASInfoCell *infoCell = (ASInfoCell *)cell;
    infoCell.delegate = self;
    
    //if (0 == indexPath.section)
    {
        [infoCell updateCellStatus:[_infoArray objectAtIndex:[indexPath row]]];
    }
    //else
    {
    //  [infoCell updateCellStatus:[_infoArray lastObject]];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

#pragma mark - ASInfoCellDelegate
- (void)selectPoliticalInfo:(ASPoliticalInfo *)info
{
    ASPoliticalInfoVC *vc = [[ASPoliticalInfoVC alloc] initWithPoliticalInfoArray:_politicalArray];
    vc.hidesBottomBarWhenPushed = YES;
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - ASPoliticalInfoVCDelegate
- (void)selectPoliInfo:(ASPoliticalInfo *)info;
{
    ASInfoCell *politicalCell = (ASInfoCell *)[_infoTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];;
    politicalCell.rightText.text = info.politicalName;
    
    _politicalNumber = info.political;
    
    [self.navigationController popViewControllerAnimated:YES];
}


@end







